package com.glory.hikvisionmobile.activity;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.glory.hikvisionmobile.R;
import com.glory.hikvisionmobile.adapter.MaterialAddAdapter;
import com.glory.hikvisionmobile.base.BaseActivity;
import com.glory.hikvisionmobile.bean.TrackOut.TrackOutLoadBean;
import com.glory.hikvisionmobile.bean.TrackOut.TrackOutSubmitBean;
import com.glory.hikvisionmobile.bean.material.XPdaMLot;
import com.glory.hikvisionmobile.bean.net.BaseResponseBean;
import com.glory.hikvisionmobile.customView.HikLabelEditText;
import com.glory.hikvisionmobile.customView.HikLabelTextView;
import com.glory.hikvisionmobile.customView.HikTabView;
import com.glory.hikvisionmobile.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * *****************************
 *
 * @Anthor yazhou
 * @Email 1184545990@qq.com
 * @Time 2020/3/26 13:56
 * @DESC *****************************
 */
public class Work_MaterialFeedingActivity extends BaseActivity {

    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.lbLotId)
    HikLabelTextView lbLotId;
    @BindView(R.id.lbMainEqp)
    HikLabelTextView lbMainEqp;
    @BindView(R.id.lbSubEqp)
    HikLabelTextView lbSubEqp;
    @BindView(R.id.lbPartDesc)
    HikLabelTextView lbPartDesc;
    @BindView(R.id.lbCurrentStep)
    HikLabelTextView lbCurrentStep;
    @BindView(R.id.lbNum)
    HikLabelTextView lbNum;
    @BindView(R.id.lbMark)
    HikLabelEditText lbMark;
    @BindView(R.id.tabView)
    HikTabView tabView;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.bottom_tv1)
    TextView bottomTv1;
    @BindView(R.id.bottom_tv2)
    TextView bottomTv2;

    MaterialAddAdapter materialAddAdapter;

    String lotId;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_work_material_feeding;
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("物料投料出站");
        return new FreeUI_AddViewEntity("header", llMain);
    }

    @Override
    protected void initView() {

        materialAddAdapter = new MaterialAddAdapter(null);

        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        recyclerview.setAdapter(materialAddAdapter);

        _initTabs();

        bottomTv1.setOnClickListener(v -> {
            trackOut();
        });
        bottomTv2.setOnClickListener(v -> {
            selectMaterial(lotId);
        });
    }

    private void _initTabs() {
        HikTabView.Tab tab1 = new HikTabView.Tab("物料名称", 2, Gravity.CENTER);
        HikTabView.Tab tab2 = new HikTabView.Tab("物料描述", 2, Gravity.CENTER);
        HikTabView.Tab tab3 = new HikTabView.Tab("数量", 2, Gravity.CENTER);
        HikTabView.Tab tab4 = new HikTabView.Tab("", 1, Gravity.CENTER);
        tabView.setTabs(tab1, tab2, tab3, tab4);
    }

    @Override
    protected void initData() {
        lotId = getIntent().getStringExtra("lotNum");
        lbLotId.getLabelContext().setText(lotId);
        loadData(lotId);
    }

    private void setViewData(TrackOutLoadBean data) {
        lbMainEqp.getLabelContext().setText(data.getMainEquipments());
        lbSubEqp.getLabelContext().setText(data.getSubEquipments());
        lbPartDesc.getLabelContext().setText(data.getPartDesc());
        lbCurrentStep.getLabelContext().setText(data.getCurrentStepDesc());
        lbNum.getLabelContext().setText(data.getMainQty()+"");
    }

    public void loadData(String lotId){
        Map map = new LinkedHashMap();
        map.put("lotId",lotId);
        NetUtils.requestMapDara("HIK.WuLiaoTouLiaoTrackOut",this,map,new FreeUI_DialogEntityCallBack<BaseResponseBean<TrackOutLoadBean>>(
                new TypeToken<BaseResponseBean<TrackOutLoadBean>>() {
                }.getType(), getSupportFragmentManager(), this){

            @Override
            public void onSuccess(Response<BaseResponseBean<TrackOutLoadBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())) {
                    setViewData(response.body().getBody());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<TrackOutLoadBean>> response) {
                super.onError(response);
                loadError(response.getException(),"HIK.WuLiaoTouLiaoTrackOut");

            }
        });
    }

    private void trackOut() {
        if (materialAddAdapter.getData() == null || materialAddAdapter.getData().size() <= 0) {
            FreeApi_ToastUtils.showFreeToast("没有添加物料", this, false, Toast.LENGTH_SHORT);
            return;
        }
        Map map = new LinkedHashMap();
        map.put("actionType", "touLiaoTrackOut");
        map.put("lotId",lotId);
        String comment = lbMark.getLabelContext().getText().toString().trim();
        if (!TextUtils.isEmpty(comment)) {
            map.put("comment", comment);
        }
        Map mLots = new LinkedHashMap();
        mLots.put("MLot", materialAddAdapter.getData());
        map.put("mLots", mLots);
        NetUtils.requestMapDara("HIK.WuLiaoTouLiaoTrackOut",this,map,new FreeUI_DialogEntityCallBack<BaseResponseBean<TrackOutSubmitBean>>(
                new TypeToken<BaseResponseBean<TrackOutSubmitBean>>() {
                }.getType(), getSupportFragmentManager(), this){

            @Override
            public void onSuccess(Response<BaseResponseBean<TrackOutSubmitBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())) {
                    String nextStepName = response.body().getBody().getNextStepDesc();
                    if (FreeApi_StringUtils.isEmpty(nextStepName)){
                        showConfirmDialog(Work_MaterialFeedingActivity.this,"出站成功，没有下一道工序");
                    }else {
                        showConfirmDialog(Work_MaterialFeedingActivity.this,"出站成功，下一道工序是:" + nextStepName);
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<TrackOutSubmitBean>> response) {
                super.onError(response);
                loadError(response.getException(),"HIK.WuLiaoTouLiaoTrackOut");
            }
        });
    }

    private void selectMaterial(String lotId){
        Intent intent = new Intent(this, Work_MaterialSelectActivity.class);
        intent.putExtra("showPlugin", "header");
        intent.putExtra("lotId", lotId);
        startActivityForResult(intent, 100);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && requestCode == 100) {
            XPdaMLot xPdaMLot = (XPdaMLot) data.getSerializableExtra("XPdaMLot");
            materialAddAdapter.addData(xPdaMLot);
        }
    }
}
